package cn.itcast.service.impl;

import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Stream;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Transactional
    @Override
    public void add(Menu menu, Integer[] permissionIds) {
        //保存菜单基础数据
        menuDao.add(menu);
        //关联保存权限信息
        if (permissionIds != null) {
            Integer menuId = menu.getId();
            setMenuAndPermission(menuId, permissionIds);
        }
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.findPage();
        List<Menu> result = page.getResult();
        if(!StringUtils.isEmpty(result)){
            for (Menu menu : result) {
                List<Menu> children = menuDao.findChildren(menu.getId());
                if (!StringUtils.isEmpty(children)){
                    menu.setChildren(children);
                }
            }
        }
        return new PageResult(page.getTotal(), result);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public Integer[] selectedPermissionId(Integer permissionId) {
        return menuDao.findPermissionAssociationIds(permissionId);
    }

    @Transactional
    @Override
    public void update(Menu menu, Integer[] permissionIds) {
        //更新菜单基础信息
        menuDao.update(menu);
        //删除菜单关联权限信息
        menuDao.deletePermissionAssociation(menu.getId());
        //设置菜单关联权限信息
        setMenuAndPermission(menu.getId(), permissionIds);
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        //删除关联信息
        menuDao.deletePermissionAssociation(id);
        //删除菜单数据
        menuDao.deleteById(id);
    }

    @Override
    public List<Menu> findAll() {

        //获取一级菜单的id
        List<Menu> menuList = menuDao.findPage();
        if(!StringUtils.isEmpty(menuList)){
            for (Menu menu : menuList) {
                List<Menu> children = menuDao.findChildren(menu.getId());
                if (!StringUtils.isEmpty(children)){
                    menu.setChildren(children);
                }
            }
        }
        return menuList;
    }

    @Override
    public Menu findRoleByUserName(String username) {
        return menuDao.findRoleByUserName(username);
    }

    /**
     * 保存菜单和权限关联信息
     * @param menuId
     * @param permissionIds
     */
    private void setMenuAndPermission(Integer menuId, Integer[] permissionIds) {
        Stream.of(permissionIds).forEach(permissionId -> menuDao.setPermissionAssociation(menuId, permissionId));
    }
}
